package com.aramis.mydytt.ui.topic.detail

import android.os.Bundle
import android.view.View
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.aramis.mydytt.R
import com.aramis.mydytt.ui.base.BaseActivity
import com.aramis.mydytt.ui.bean.MovieSpecialBean
import com.aramis.mydytt.ui.bean.MovieSpecialDetailBean
import com.aramis.mydytt.ui.detail.MovieDetailActivity

/**
 * Description: TopicDetailActivity
 * @author lizhidan
 * Created: 2021/5/21
 */
class TopicDetailActivity : BaseActivity() {
    private lateinit var data: MovieSpecialBean
    private lateinit var viewModel: TopicDetailViewModel
    private val adapter = TopicDetailListAdapter()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_topic_detail)
        initView()
        initData()
    }

    private fun initView() {
        viewModel = createViewModel()
        data = intent.getSerializableExtra("data") as MovieSpecialBean
        val recyclerView = findViewById<RecyclerView>(R.id.recycler_topic_detail)
        recyclerView.adapter = adapter
        val layoutManager = GridLayoutManager(this, 3)
        layoutManager.spanSizeLookup = object : GridLayoutManager.SpanSizeLookup() {
            override fun getSpanSize(position: Int): Int {
                return if (position == 0) 3 else 1
            }
        }
        recyclerView.layoutManager = layoutManager
        adapter.onMovieItemClickListener = {
            MovieDetailActivity.start(this, it.toMovieItemBean())
        }
        findViewById<View>(R.id.image_back).setOnClickListener { finish() }
    }

    private fun initData() {
        viewModel.topicDetailLiveData.observe(this, {
            if (it != null) {
                showContentView(it)
            } else {
                showErrorView()
            }
        })
        viewModel.getTopicDetail(data.id)
    }

    private fun showContentView(bean: MovieSpecialDetailBean) {
        adapter.setDataBean(bean)
    }

    private fun showErrorView() {

    }
}